*************************************************************************
*																		*
*				Discrete Survival Analysis 								*
*																		*
*************************************************************************

* Project: "Voting in Context" Book
* Title: "Always late? Stability and change in individuals‘ time of vote decisions"
* Author: Maria Preißinger 
* Date: Feb 19, 2016

set more off
				
	
*-----------------------------------------------
* Table 5: Model for each year separately
*-----------------------------------------------


estimates clear
foreach year in a b {
quietly logit `year'_decided if !missing(fd_`year'_panelcongtalk, ///
	fd_`year'_panelcongcontact, fd_`year'_panelcongtvduell, ///
	fd_`year'_panelcongard, fd_`year'_panelcongzdf, ///
	fd_`year'_talk, fd_`year'_countcontact, fd_`year'_tvduell, ///
	fd_`year'_paper, ///
	fd_`year'_oeff, wavefd)
scalar ll0 = `e(ll)'	//Log-Likelihood

eststo: logit `year'_decided  ///
	i.fd_`year'_panelcongtalk i.fd_`year'_panelcongcontact ///
	i.fd_`year'_panelcongtvduell ///
	i.fd_`year'_panelcongard i.fd_`year'_panelcongzdf ///
	fd_`year'_talk fd_`year'_countcontact i.fd_`year'_tvduell ///
	fd_`year'_paper ///
	fd_`year'_oeff ibn.wavefd, noconstant
scalar llf = `e(ll)' //Log-Likelihood
estadd scalar mc = 1-(llf/ll0) //computes McFadden's R²
egen `year'_obs = group(newid0913) if e(sample)==1
sum `year'_obs		//# of individuals
estadd scalar ind = r(max)
drop `year'_obs
}	


esttab est1 est2 using "table 5.rtf", ///
	b(2) drop(0.*) se stats(N ind mc, fmt(0 0 2)) replace
	

*------------------------------------------------------
* Table 6: Model intraindividual comparison between years
*-------------------------------------------------------


mlogit diff_decided diff_panelcongtalk ///
	diff_panelcongcontact diff_paneltalk diff_panelcountcontact ///
	diff_panelpaper diff_paneloeff ibn.wavem, noconstant
	
/* If someone is undecided or decided in both years, we do not expect 
a difference in the independent variables. 
That's why we combine categories */

gen help_decided = diff_decided
replace help_decided = 4 if diff_decided==0 | diff_decided==3 

estimates clear

mlogit help_decided if !missing(diff_panelcongtalk, ///
	diff_panelcongcontact, diff_panelcongtvduell, diff_panelcongard, ///
	diff_panelcongzdf, ///
	diff_paneltalk, diff_tvduell, diff_panelcountcontact, ///
	diff_panelpaper, diff_paneloeff)
scalar ll0 = `e(ll)'

eststo: mlogit help_decided diff_panelcongtalk ///
	diff_panelcongcontact diff_panelcongtvduell ///
	diff_panelcongard diff_panelcongzdf ///
	diff_paneltalk diff_panelcountcontact ///
	diff_panelpaper diff_tvduell ///
	diff_paneloeff ibn.wavem, noconstant base(4)
scalar llf = `e(ll)'
estadd scalar mc = 1-(llf/ll0)
egen diff_obs = group(newid0913) if e(sample)==1
sum diff_obs		//# of individuals
estadd scalar ind = r(max)

esttab est1 using "table 6.rtf" , ///
	replace unstack b(2) se  ///
	eqlabel("2009 decided, 2013 undecided" "2013 decided, 2009 undecided") ///
	stats(N ind mc, fmt(0 0 2)) nonumber

	